<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Links_handle extends MY_Controller
{
    function get_list()
    {
        $gid = ( int ) $this->input->get( 'gid' );
        
        $begin = ( int ) $this->input->get( 'begin' );
        $length = ( int ) $this->input->get( 'length' );
        
        $begin = $begin < 0 ? 0 : $begin;
        $length = $length < 1 ? 100 : $length;
        
        $this->load->model( 'links_model' );
        $count = $this->links_model->get_list_count( $gid );
        if ($begin > 0 && $begin >= $count)
        {
            $this->rs->err( 4 );
        }
        $list = $this->links_model->get_list( $gid, $length, $begin );
        $this->rs->ok( array (
                'record_count' => $count,
                'list' => $list 
        ) );
    }
    function get()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'links_model' );
        if ($content = $this->links_model->get( $id ))
        {
            $this->rs->ok( $content );
        }
        $this->rs->err( '内容不存在！' );
    }
    function modify()
    {
        $id = ( int ) $this->input->post( 'id' );
        $is_insert = $id > 0 ? false : true;
        $modify_data = array ();
        
        $deleted = $this->input->post( 'deleted' );
        if ($is_insert || isset( $deleted ))
        {
            $modify_data['deleted'] = $deleted ? 1 : 0;
        }
        
        $sub_id = $this->input->post( 'sub_id' );
        if (isset( $sub_id ))
        {
            $modify_data['sub_id'] = ( int ) $sub_id;
        }
        
        $target = $this->input->post( 'target' );
        if ($is_insert || isset( $target ))
        {
            $modify_data['target'] = $target ? 1 : 0;
        }
        
        $uri = $this->input->post( 'uri' );
        if (isset( $uri ))
        {
            $modify_data['uri'] = substr_utf8( trim( $uri, ' /　' ), 255 );
        }
        
        $selected_uri = $this->input->post( 'selected_uri' );
        if (isset( $selected_uri ))
        {
            $modify_data['selected_uri'] = substr_utf8( $selected_uri, 255 );
        }
        
        $img_cover = $this->input->post( 'img_cover' );
        if (isset( $img_cover ))
        {
            $modify_data['img_cover'] = substr_utf8( $img_cover, 255 );
        }
        
        $title = $this->input->post( 'title' );
        if ($is_insert || isset( $title ))
        {
            if (empty( $title ))
            {
                $this->rs->err( '名称或标题不能为空' );
            }
            $modify_data['title'] = substr_utf8( $title, 255 );
        }
        
        $subtitle = $this->input->post( 'subtitle' );
        if (isset( $subtitle ))
        {
            $modify_data['subtitle'] = substr_utf8( $subtitle, 255 );
        }
        
        $description = $this->input->post( 'description' );
        if (isset( $description ))
        {
            $modify_data['description'] = substr_utf8( $description, 2000 );
        }
        
        $en_description = $this->input->post( 'en_description' );
        if (isset( $en_description ))
        {
            $modify_data['en_description'] = substr_utf8( $en_description, 2000 );
        }
        
        $ext = $this->input->post( 'ext' );
        if (isset( $ext ))
        {
            $modify_data['ext'] = substr_utf8( $ext, 255 );
        }
        
        $sort = $this->input->post( 'sort' );
        if ($is_insert || isset( $sort ))
        {
            $modify_data['sort'] = ( int ) $sort;
        }
        
        $enable = $this->input->post( 'enable' );
        if ($is_insert || isset( $enable ))
        {
            $modify_data['enable'] = $enable ? 1 : 0;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        
        $this->load->model( 'links_model' );
        if ($is_insert)
        {
            $gid = $this->input->post( 'gid' );
            $modify_data['gid'] = ( int ) $gid;
            if ($this->links_model->insert( $modify_data ))
            {
                $this->rs->ok();
            }
        }
        else
        {
            if (false !== $this->links_model->update( $modify_data, $id ))
            {
                $this->rs->ok();
            }
        }
        $this->rs->err();
    }
}